package com.zx.mes.hyl.auth.dao;



import com.zx.mes.hyl.auth.model.Trole;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author hyl
 * @date 2017-11-25
 */
public interface RoleRepository extends JpaRepository<Trole,String>,JpaSpecificationExecutor<Trole> {
    @Query("select distinct t from Trole t left join fetch t.tresources resource where t.id = :id")
    Trole getById(@Param("id") String id);

    @Query("select distinct t from Trole t left join fetch t.tresources resource join fetch t.tusers user where user.id = :userId order by t.seq")
    List<Trole> findByUserId(@Param("userId") String userId);

    @Query("select distinct t from Trole t join fetch t.tusers user where user.id = :userId order by t.seq")
    List<Trole> findAllRoleByUserId(@Param("userId") String userId);

    /**
     * 根据角色名称找记录
     * @param name
     * @return role
     */
    Trole findByName(String name);

    /**
     *
     * @param name
     * @return list
     */
    List<Trole> getAllByName(String name);

    /**
     * 根据角色id找寻记录
     * @param name
     * @param remark
     * @param id
     * @return int
     */
    @Transactional
    @Modifying
    @Query("update Trole t set t.name = ?1,t.remark=?2,t.alias=?3 where t.id = ?4")
    int updateById(String name, String remark, String alias, String id);

    /**
     *
     * @param name
     * @param remark
     * @param alias
     * @param trole
     * @param id
     * @return
     */
    @Transactional
    @Modifying
    @Query("update Trole t set t.name = ?1,t.remark=?2,t.alias=?3,t.trole=?4 where t.id = ?5")
    int updateById2(String name, String remark, String alias, Trole trole, String id);

    /**
     * 根据角色名称模糊查询并且分页
     * @param name
     * @param pageable
     * @return list
     */
    List<Trole> findByNameLike(String name, Pageable pageable);
}

